<?php
/**
 * appRain v 0.1.x
 *
 * LICENSE
 *
 * This source file is subject to the MIT license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.opensource.org/licenses/mit-license.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@apprain.com so we can send you a copy immediately.
 *
 * @copyright  Copyright (c) 2010 appRain, Team. (http://www.apprain.com)
 * @license    http://www.opensource.org/licenses/mit-license.php MIT license
 *
 * HELP
 *
 * Official Website
 * http://www.apprain.com/
 *
 * Download Link
 * http://www.apprain.com/download
 *
 * Documents Link
 * http ://www.apprain.com/docs
 */

class Development_Callbacks extends appRain_Base_Modules_Callbacks
{
    /*
     * Callback function
     *
     * Run before page start
     */
    public function before_render()
    {

        $protomAlo = App::Plugin('RSS_Reader')->setFeedUri("http://www.prothom-alo.com/rss")->Parse();
        $this->set('protomAlo',$protomAlo);

        $bbc = App::Plugin('RSS_Reader')->setFeedUri("http://www.bbc.co.uk/bengali/index.xml")->Parse(true);
        $this->set('bbc',$bbc);

        $weather = $this->WeatherData("http://www.weatherforecastmap.com/bangladesh/dhaka/rss.xml","Dhaka");
        $weather .= $this->WeatherData("http://www.weatherforecastmap.com/bangladesh/sylhet/rss.xml","Sylhet");
        $weather .= $this->WeatherData("http://www.weatherforecastmap.com/bangladesh/chittagong/rss.xml","Chittagong");
        $weather .= $this->WeatherData("http://www.weatherforecastmap.com/bangladesh/khulna/rss.xml","Khulna");
        $weather .= $this->WeatherData("http://www.weatherforecastmap.com/bangladesh/barisal/rss.xml","Barisal");
        $weather .= $this->WeatherData("http://www.weatherforecastmap.com/bangladesh/jessore/rss.xml","Jessore");
        $weather .= $this->WeatherData("http://www.weatherforecastmap.com/bangladesh/bogra/rss.xml","Bogra");

        $this->set('weather',$weather);

        $pAloLinks = $this->pAloLinks();
        $this->set('pAloLinks',$pAloLinks);

         $palodate = $this->palodate();
        $this->set('palodate',$palodate);

    }

    public function WeatherData($url="",$title=""){
        $weather = App::Plugin('RSS_Reader')->setFeedUri($url)->Parse(true);
        $str = (string) $weather->channel->item->description;
        $arr = preg_split("[\<b\>|\<\/b\>|src=\"|\"|\<\b\r\ \/\>]",$str);
        if(isset($arr[3]) && $arr[5]){
            $arr[6] = str_replace("</p>","",$arr[6]);
            return "<div><img width=\"36\" height=\"36\" src=\"{$arr[3]}\" /> <b>{$title}</b> {$arr[6]}</div>";
        }
    }

    public function pAloLinks(){
        $page = App::Plugin('RSS_Reader')->setFeedUri('http://www.prothom-alo.com')->Parse('text');
        $tmparr = preg_split("[\<li class\=\"\"\>]",$page);
        return  $this->getLinsInfo($tmparr);

    }

     public function palodate(){
        $page = App::Plugin('RSS_Reader')->setFeedUri('http://www.prothom-alo.com')->Parse('text');

        $tmparr = preg_split("[\<div class=\"widgetTextTitle\">]",$page);
        $tmparr = preg_split("[\</div>]",$tmparr[1]);
        return $tmparr[0];
               

    }

    private function getLinsInfo($data){
        $links = array();
        foreach($data as $row){
            if(substr($row,0,9) == '<a href="'){
               $arr = preg_split("[\"]",$row);
               $arr[2] = strip_tags("<{$arr[2]}");
               $links[] = array('link'=>'http://www.prothom-alo.com' . $arr[1],'title'=>$arr[2]);
            }
        }
        return ($links);
    }

    /*
     * Callback function
     *
     * Run before admin start
     */
    public function before_adminpanel_render()
    {
        $collapseAdminLeftPan = App::Session()->Read('collapseAdminLeftPan');
        $this->setIsAdminLeftPanCollapse($collapseAdminLeftPan);
        
        ## Sample noitice for admin
        //App::Module('Notification')->Push($this->__("Sample notice for admin"),array('type'=>'admin-notice','level'=>'Notice'));
    }

    /*
     * Callback function
     *
     * Run after page render complete
     */
    public function after_render()
    {
    }

    /*
     * Callback function
     *
     * Run before admin start
     */
    public function after_adminpanel_render()
    {
    }

    /**
     * Callback function
     *
     * Before an addon load complete
     */
    public function before_addon_load($name = NULL, $options = NULL)
    {
        switch( $name)
        {

        }
    }

    /**
     * Callback function
     *
     * After an addon load
     */
    public function after_addon_load($name = NULL, $options = NULL)
    {
        switch( $name)
        {
            case "rich_text_editor" :
                                       if( $this->get_config('rich_text_editor') == 'Yes')
                                        {
                                            $rich_text_editor_mode = $this->get_config('rich_text_editor_mode');
                                            $rich_text_editor_mode = isset($rich_text_editor_mode) ? $rich_text_editor_mode : "Simple";
                                            $tool_bar_btns = "";

                                            if( $rich_text_editor_mode == 'Simple' )
                                            {
                                                $tool_bar_btns = ", toolbar :
                                                                    [
                                                                        ['Source'],['Maximize'],['Styles','Format','Font','FontSize'],['TextColor','BGColor'],
                                                                        '/',
                                                                        ['Bold','Italic','Underline','Strike','-','Subscript','Superscript'],
                                                                        ['NumberedList','BulletedList'],['JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock'],
                                                                        ['Link','Unlink','Anchor'],['Image','Table','Smiley','SpecialChar','SpellChecker']
                                                                    ]";
                                             }

                                            echo "<script type=\"text/javascript\">
                                                //<![CDATA[
                                                    window.onload = function()
                                                    {
                                                        var arr = document.getElementsByTagName('textarea');
                                                        for(var i = 0; i < arr.length; i++ )
                                                        {
                                                           if( arr[i].className.match('richtexteditor'))
                                                           {
                                                                CKEDITOR.replace(arr[i], {
                                                                    extraPlugins : 'uicolor',
                                                                    uiColor: '#D9E5EE'
                                                                    {$tool_bar_btns}
                                                                });
                                                            }
                                                        }
                                                    }
                                                 //]]>
                                                </script>";
                                        }
                                        break;
        }
    }

    /**
    * Information Set call back function.
    * Run when we view information set
    *
    * @param fixedArray
    */
    public function on_information_set_view($options = NULL)
    {
        switch( $options['type'])
        {

        }
    }

    /*
     * Callback function
     *
     * Run when any Information Set entry delete
     */
    public function on_information_set_delete($options = NULL)
    {
        switch( $options['type'])
        {

        }
    }

    /*
     * Callback function
     *
     * Run before Information Set entery Save
     */
    public function before_information_set_save($options = NULL)
    {
        switch( $options['type'])
        {

        }
    }

    /*
     * Callback function
     *
     * Run after Information Set entry save
     */
    public function after_information_set_save($options = NULL)
    {
        switch( $options['type'])
        {

        }
    }

    /*
     * Callback function
     *
     * Run before search data initialized
     * Debug hints
     * pre($send->searchPool)
     * pre($send->getSearchstring());
     * pre($send->getLimit());
     * pre($send->getGroupnames());
     */
    public function _before_search_init($send = NULL)
    {

        /*$send->searchPool[] = Array('id'=>'5','str'=>'xy1');
        $send->searchPool[] = Array('id'=>'6','str'=>'xy2');
        $send->searchPool[] = Array('id'=>'7','str'=>'xy3');*/

    }

    /*
     * Callback function
     *
     * Run once auto search initialization complete
     */
    public function after_search_init($send = NULL)
    {
    }

    /*
     * Callback function
     *
     * Helps to modify the URL Manager Definition
     */
    public function on_uri_definition_init($def=NULL)
    {
        /*
         * Example 1
         * This example moves control to
         * home/index for the url www.example.com/hellow-world
         */
        $def['pagerouter'][] = array(
                         "actual"=>Array("home","index"),
                         "virtual"=>Array("hello-world1"));

        /*
         * Example 2
         * This example moves control to
         * home/index/5 for the url www.example.com/birthday-gift/15th-birthday
         */
        /*$def['pagerouter'][] = array(
                         "actual"=>Array("home","index","5"),
                         "virtual"=>Array("birthday-gift","15th-birthday"));*/

        /*
         * For any new chnage we have to return $def
         */
        //return $def;
    }
}